
#include "opencv2/opencv.hpp"
#include <iostream>  
#include <fstream>  
#include <stdio.h>
#include<string>
#include "opencv2/imgproc/types_c.h"
//修改后的版本，能得出所有角点的参数，以及每张图片第一个角点的参数
using namespace cv;
using namespace std;

ofstream fout("caliberation_result.txt");  /* 保存标定结果的文件 */
string filename;

int image_index = 0; //图像序号
const int maxIndex = 12;//文件夹下最大的图像张数
Size image_size;  //图像的尺寸（像素尺寸）
Size board_size = Size(8, 11);   // 棋盘格每行、列的角点数（不是棋盘格行列个数）
vector<Point2f> image_corners;  // 每幅图像上检测到的角点数组
vector<vector<Point2f>> all_corners; //所有图像角点数组
const string filePath = "";//文件夹路径

Size cell_size = Size(15.0, 15.0);  //每个棋盘格的物理尺寸大小
vector<vector<Point3f>> object_points;// 棋盘格角点的三维坐标数组
Mat cameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); // 摄像头内参数矩阵
Mat distCoeffs = Mat(1, 5, CV_32FC1, Scalar::all(0)); //摄像头5个畸变系数：k1,k2,p1,p2,k3
vector<Mat> tvecsMat;  //图像的旋转向量数组
vector<Mat> rvecsMat;//图像的平移向量数组


string addInexToName(int index, string fileDirectory, string tail);
string addInexToName1(int index, string fileDirectory, string tail);
void getImageCorners();
void getImageCorners1();
void cameraCalibrate();
void outputCalibrateResult();
void undistortImages();
void reProject();
void reProjectSingle(int m);
void undistortSingleImages(int i);

int main()
{
	getImageCorners();//获得图像亚像素角点
	cameraCalibrate();//摄像头标定，得出内参外参
	outputCalibrateResult();//将标定结果保存到文件
	undistortImages();//矫正图像
	reProject();//生成每张图像的鸟瞰图

	undistortSingleImages(0);//针对实际场景标定使用的棋盘格图像进行畸变矫正
	getImageCorners1();//获取实际场景中标定使用的棋盘格的角点坐标，用于投影变换取值
	undistortSingleImages(1);//针对实际场景需要测量的图像进行畸变矫正	
	reProjectSingle(1);//针对实际场景需要测量的图像进行投影变换	
	return 0;
}


string addInexToName(int index, string fileDirectory, string tail) {
	char buff[200];
	sprintf_s(buff, "%02d", index);
	string m_int2str(buff);
	string name = filePath + fileDirectory + "//" + m_int2str + tail;
	return name;
}

void getImageCorners()
{
	cout << "开始提取角点......\n";
	while (1)
	{
		
		if (image_index == maxIndex) break;
		image_index++;//从1-12，共12个数字
		Mat imageInput = imread(addInexToName(image_index, "pic", ".jpg"));
		//读入第一张图片时获取图像宽高信息  
		if (image_index == 1)
		{
			image_size.width = imageInput.cols;
			image_size.height = imageInput.rows;
		}
		//提取角点
		if (findChessboardCorners(imageInput, board_size, image_corners) == false)
		{
			cout << "can not find chessboard corners  in   " << addInexToName(image_index, "pic", ".jpg") << "!\n"; //找不到角点  
			exit(1);
		}
		else
		{
			Mat view_gray;
			cvtColor(imageInput, view_gray, CV_RGB2GRAY);
			//亚像素精确化
			find4QuadCornerSubpix(view_gray, image_corners, board_size);
			all_corners.push_back(image_corners);
			//在图像上显示角点位置
			drawChessboardCorners(imageInput, board_size, image_corners, false);
			imshow("Camera Calibration", imageInput);
			imwrite(addInexToName(image_index, "pic", "_a.jpg"), imageInput);
			waitKey(100);
		}
	}

	int total = all_corners.size();
	int a = 0;
	for (auto it = all_corners.cbegin(); it != all_corners.cend(); ++it)
	{
		a++;
		cout<<*it<<"corners  a:"<<a<<endl;
	}
	cout << "total = " << total << endl;
	int CornerNum = board_size.width * board_size.height;  //每张图片上总的角点数
	for (int ii = 0; ii < total; ii++)
	{
		if (0 == ii % CornerNum)// 88 是每幅图片的角点个数。此判断语句是为了输出 图片号，便于控制台观看 
		{
			int i = -1;
			i = ii / CornerNum;
			int j = i + 1;
			cout << "--> 第 " << j << "图片的数据 --> : " << endl;
		}
		if (0 == ii % 3)	// 此判断语句，格式化输出，便于控制台查看
		{
			cout << endl;
		}
		else
		{
			cout.width(10);
		}
		//输出所有的角点
		cout << " -->" << all_corners[ii][0].x;
		cout << " -->" << all_corners[ii][0].y;
	}

	cout << "角点提取完成！\n";

	/*内外参数*/
	vector<int> point_counts;  // 每幅图像中角点的数量
	/* 初始化标定板上角点的三维坐标 */
	int i, j, t;
	for (t = 0; t < image_index; t++)
	{
		vector<Point3f> tempPointSet;
		for (i = 0; i < board_size.height; i++)
		{
			for (j = 0; j < board_size.width; j++)
			{
				Point3f realPoint;
				/* 假设标定板放在世界坐标系中z=0的平面上 */
				realPoint.x = i * cell_size.width;
				realPoint.y = j * cell_size.height;
				realPoint.z = 0;
				tempPointSet.push_back(realPoint);
			}
		}
		object_points.push_back(tempPointSet);
	}
	cout << "test………………\n" << image_size << endl;
	for (i = 0; i < image_index; i++)
	{
		point_counts.push_back(board_size.width * board_size.height);
	}
	cout << "image_index :" << image_index <<endl;
	/* 开始标定 */
	calibrateCamera(object_points, all_corners, image_size, cameraMatrix, distCoeffs, rvecsMat, tvecsMat, 0);
	cout << "标定完成！\n";
	//对标定结果进行评价
	cout << "开始评价标定结果………………\n";
	double total_err = 0.0; /* 所有图像的平均误差的总和 */
	double err = 0.0; /* 每幅图像的平均误差 */
	vector<Point2f> image_points2; /* 保存重新计算得到的投影点 */
	cout << "\t每幅图像的标定误差：\n";
	fout << "每幅图像的标定误差：\n";
	for (i = 0; i < image_index; i++)
	{
		vector<Point3f> tempPointSet = object_points[i];
		/* 通过得到的摄像机内外参数，对空间的三维点进行重新投影计算，得到新的投影点 */
		projectPoints(tempPointSet, rvecsMat[i], tvecsMat[i], cameraMatrix, distCoeffs, image_points2);
		/* 计算新的投影点和旧的投影点之间的误差*/
		vector<Point2f> tempImagePoint = all_corners[i];
		Mat tempImagePointMat = Mat(1, tempImagePoint.size(), CV_32FC2);
		Mat image_points2Mat = Mat(1, image_points2.size(), CV_32FC2);
		for (int j = 0; j < tempImagePoint.size(); j++)
		{
			image_points2Mat.at<Vec2f>(0, j) = Vec2f(image_points2[j].x, image_points2[j].y);
			tempImagePointMat.at<Vec2f>(0, j) = Vec2f(tempImagePoint[j].x, tempImagePoint[j].y);
		}
		err = norm(image_points2Mat, tempImagePointMat, NORM_L2);
		total_err += err /= point_counts[i];
		std::cout << "第" << i + 1 << "幅图像的平均误差：" << err << "像素" << endl;
		fout << "第" << i + 1 << "幅图像的平均误差：" << err << "像素" << endl;
	}
	std::cout << "总体平均误差：" << total_err / image_index << "像素" << endl;
	fout << "总体平均误差：" << total_err / image_index << "像素" << endl << endl;
	std::cout << "评价完成！" << endl;
}


void cameraCalibrate()
{
	cout << "开始标定......\n";
	//初始化标定板上角点的三维坐标
	int i, j, t;
	for (t = 1; t < image_index; t++)
	{
		vector<Point3f> position;
		for (i = 0; i < board_size.height; i++)
		{
			for (j = 0; j < board_size.width; j++)//行优先存储
			{
				Point3f realPoint;//假设标定板放在世界坐标系中z=0的平面上				
				realPoint.x = (float)i * cell_size.width;//第一个点为原点
				realPoint.y = (float)j * cell_size.height;
				realPoint.z = (float)0;
				position.push_back(realPoint);
			}
		}
		//object_points.push_back(position);
	}
	//开始标定
	calibrateCamera(object_points, all_corners, image_size, cameraMatrix, distCoeffs, rvecsMat, tvecsMat, 0);
	cout << "标定完成！\n";
}


void outputCalibrateResult()
{
	cout << "开始保存标定结果..." << endl;
	ofstream fout("result.txt"); // 保存标定结果的文件
	Mat rotation_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); //保存每幅图像的旋转矩阵
	fout << "摄像头内参矩阵：" << endl;
	fout << cameraMatrix << endl << endl;
	fout << "摄像头畸变系数：\n";
	fout << distCoeffs << endl << endl << endl;
	for (int i = 0; i < image_index; i++)
	{
		Rodrigues(tvecsMat[i], rotation_matrix);//将旋转向量转换为相对应的旋转矩阵
		fout << "第" << i + 1 << "幅图像的旋转矩阵：" << endl;
		fout << rotation_matrix << endl;
		fout << "第" << i + 1 << "幅图像的平移向量：" << endl;
		fout << rvecsMat[i] << endl << endl;
	}
	fout << endl;
	cout << "保存完成！" << endl;
}

void undistortImages()
{
	cout << "开始矫正图像..." << endl;
	Mat mapx = Mat(image_size, CV_32FC1);
	Mat mapy = Mat(image_size, CV_32FC1);
	Mat R = Mat::eye(3, 3, CV_32F);
	for (int i = 1; i != image_index+1; i++)
	{
		initUndistortRectifyMap(cameraMatrix, distCoeffs, R, cameraMatrix, image_size, CV_32FC1, mapx, mapy);
		Mat imageInput = imread(addInexToName(i, "pic", ".jpg"));
		Mat imageCorrect = imageInput.clone();
		remap(imageInput, imageCorrect, mapx, mapy, INTER_LINEAR);
		imwrite(addInexToName(i, "pic", "_b.jpg"), imageCorrect);
	}
	cout << "矫正结束" << endl;
}

void reProject()
{
	cout << "开始生成鸟瞰图..." << endl;
	int board_w = board_size.width;
	int board_h = board_size.height;
	//对每幅图像进行处理
	for (int i = 1; i != image_index+1; i++)
	{
		//找到单应矩阵
		Mat h = Mat(3, 3, CV_32F, Scalar::all(0));
		//选定的4对顶点
		vector<Point2f> objPts(4);
		vector<Point2f> imgPts(4);
		//每对顶点在顶点数组中的index
		int indexArray[4] = {
			0,//左上角（0,0） //0号
			board_w - 1,//右上角（w-1,0）
			(board_h - 1) * board_w,//左下角（0,h-1） 
			(board_h - 1) * board_w + board_w - 1//右上角（w-1,h-1） 
		};
		//给选定的4对顶点赋值：必须是point2f类型，所以objPts只取x,y坐标
		for (int j = 0; j < 4; j++) {
			objPts[j].x = object_points[i-1][indexArray[j]].x * 2;
			objPts[j].y = object_points[i-1][indexArray[j]].y * 2;
			imgPts[j] = all_corners[i-1][indexArray[j]];
		}
		h = getPerspectiveTransform(objPts, imgPts);
		Mat imageInput = imread(addInexToName(i, "pic", ".jpg"));
		Mat birdImage = imageInput.clone();
		//使用单应矩阵来remap view
		warpPerspective(imageInput, birdImage, h, image_size, CV_INTER_LINEAR + CV_WARP_INVERSE_MAP + CV_WARP_FILL_OUTLIERS);
		imwrite(addInexToName(i, "pic", "_c.jpg"), birdImage);
	}
	cout << "鸟瞰图生成结束" << endl;
}

void undistortSingleImages(int i)
{
	cout << "开始矫正单张图像..." << endl;
	Mat mapx = Mat(image_size, CV_32FC1);
	Mat mapy = Mat(image_size, CV_32FC1);
	Mat R = Mat::eye(3, 3, CV_32F);

	initUndistortRectifyMap(cameraMatrix, distCoeffs, R, cameraMatrix, image_size, CV_32FC1, mapx, mapy);
	Mat imageInput = imread(addInexToName1(i, "pic", ".jpg"));
	Mat imageCorrect = imageInput.clone();
	remap(imageInput, imageCorrect, mapx, mapy, INTER_LINEAR);
	imwrite(addInexToName1(i, "pic", "_b.jpg"), imageCorrect);

	cout << "矫正单张图像结束" << endl;
}

string addInexToName1(int index, string fileDirectory, string tail)
{
	char buff[200];
	sprintf_s(buff, "Test%02d", index);
	string m_int2str(buff);
	string name = filePath + fileDirectory + "//" + m_int2str + tail;
	return name;
}

void getImageCorners1()
{

	cout << "开始提取单个图像角点......\n";
	all_corners.clear();

	Mat imageInput = imread(addInexToName1(0, "pic", "_b.jpg"));
	//提取角点
	if (findChessboardCorners(imageInput, board_size, image_corners) == false)
	{
		cout << "can not find chessboard corners  in   " << addInexToName1(0, "pic", ".jpg") << "!\n"; //找不到角点  
		exit(1);
	}
	else
	{
		Mat view_gray;
		cvtColor(imageInput, view_gray, CV_RGB2GRAY);
		//亚像素精确化
		find4QuadCornerSubpix(view_gray, image_corners, board_size);
		all_corners.push_back(image_corners);
		//在图像上显示角点位置
		drawChessboardCorners(imageInput, board_size, image_corners, false);
		imshow("Camera Calibration", imageInput);
		imwrite(addInexToName1(0, "pic", "_c.jpg"), imageInput);
		waitKey(100);//不加这句，imshow会显示灰屏
	}

	cout << "角点提取完成！\n";
}

void reProjectSingle(int m)
{
	cout << "开始生成单个鸟瞰图..." << endl;
	int board_w = board_size.width;
	int board_h = board_size.height;
	//对每幅图像进行处理

	//找到单应矩阵
	Mat h = Mat(3, 3, CV_32F, Scalar::all(0));
	//选定的4对顶点
	vector<Point2f> objPts(4);
	vector<Point2f> imgPts(4);

	int i = 0;
	//每对顶点在顶点数组中的index
	int indexArray[4] = {
		0,//左上角（0,0） //0号
		board_w - 1,//右上角（w-1,0）//8号
		(board_h - 1) * board_w,//左下角（0,h-1） //5x9=45号
		(board_h - 1) * board_w + board_w - 1//右上角（w-1,h-1） //5*9+8=53号
	};
	//给选定的4对顶点赋值：必须是point2f类型，所以objPts只取x,y坐标
	for (int j = 0; j < 4; j++) {
		objPts[j].x = object_points[i][indexArray[j]].x * 2;
		objPts[j].y = object_points[i][indexArray[j]].y * 2;
		imgPts[j] = all_corners[i][indexArray[j]];
	}
	h = getPerspectiveTransform(objPts, imgPts);
	Mat imageInput = imread(addInexToName1(m, "pic", "_b.jpg"));
	Mat birdImage = imageInput.clone();
	//使用单应矩阵来remap view
	warpPerspective(imageInput, birdImage, h, image_size, CV_INTER_LINEAR + CV_WARP_INVERSE_MAP + CV_WARP_FILL_OUTLIERS);
	imwrite(addInexToName1(m, "pic", "_d.jpg"), birdImage);


	cout << "鸟瞰图生成结束" << endl;
}